#ifndef MULTITIMER_H_MYIDENTIFY_1985
#define MULTITIMER_H_MYIDENTIFY_1985
#include <boost/function.hpp>
namespace threadpool
{
    typedef boost::function<void()> task_type;
    //used for multi timers.
    class multitimer
    {
    private:
        task_type m_timertask;
        int m_delaysec;
        int m_elapse;
        bool m_inuse;
        bool m_repeat;
        int m_timerid;                   //the unique id for each timer.
        static int timer_counter;
    public:
        multitimer(const task_type& task=NULL,int delaysec=0,bool repeat=false)
            :m_timertask(task),m_delaysec(delaysec),
            m_elapse(0),m_repeat(repeat)
        {
            m_inuse = (m_timertask==NULL)?false:true;
            m_timerid = ++timer_counter;
        }
        void add_elapse(int elapse)
        {
            if(!inuse())
                return;
            if(m_elapse>m_delaysec)
                return;
            m_elapse += elapse;
        }
        bool isready()
        {
            if(!inuse())
                return false;
            if(m_elapse>=m_delaysec)
            {
                if(m_timertask!=NULL)
                    return true;
                m_inuse = false;
            }
            return false;
        }
        bool inuse()
        {
            return m_inuse;
        }
        task_type& get_timertask_and_reset()
        {
            m_elapse = 0;
            if(!m_repeat)
            {
                m_inuse = false;
            }
            return m_timertask;
        }
        int getuniqueid()
        {
            return m_timerid;
        }
        void deletetimer()
        {
            m_elapse = 0;
            m_timertask = NULL;
            m_inuse = false;
        }
    };
    int multitimer::timer_counter = 0;
}

#endif
